<template>
	<view class="padding-30">
		<view class="gift-list">
			<view class="gift-item" v-for="(item,index) in giftList" :key="index">
				<view class="gift-item-icon" v-if="item.prize">待领取</view>
				<image class="gift-item-image" :src="item.img" mode=""></image>
				<view class="gift-item-desc">
					<view class="gift-item-title">{{item.name}}</view>
					<view class="gift-item-bottom">
						<view class="gift-item-price">￥{{item.amount}}</view>
						<!-- <button-round @click="goToPay(item)" :name="item.prize?'领取':'红包兑换'" 
						fontSize="12px" borderRadius="10rpx"></button-round> -->
						<view class="gift-btn-item" @click="goToPay(item)">{{item.prize?'领取':'兑换'}}</view>
					</view>
					<!-- 兑换和领取奖品弹窗 -->
					<popup-modal :popupObject="popupObject" @closeModel="closeModel" :popShow="show" @clickShow="popUpViewShow(item)"></popup-modal>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import buttonRound from "@/components/button/button-round.vue"
	import popupModal from "@/components/popup/popup-modal.vue"
	export default{
		components:{
			buttonRound,
			popupModal
		},
		data(){
			return{
				giftList:[],
				show: false,
				popupObject:{
					title:'是否',
					desc:'',
					btn:'确认',
					tips:''
				},
				form1:{},
				form:{},
				giftItem:{}
			}
		},
		onShow() {
			this.getShopList()
		},
		methods:{
			closeModel(){
				this.show = false
			},
			// 确认兑换或领取礼品
			popUpViewShow(){
				if(this.giftItem.prize){
					this.show = false
				    this.form1 = {
						order_type:7,
						type:3,
						prize_id:this.giftItem.prize
					}
					this.credentials()
				}else{
					this.show = false
					this.form1 = {
						order_type:5,
						type:3,
						shop_id:this.giftItem.id
					}
					this.credentials()
				}
			},
			// 领取和兑换
			goToPay(item){
				// 判断领取和兑换
				if(item.prize){
				    this.show = true
					this.popupObject.desc = '领取该礼品'
					this.giftItem = item
				}else{
					this.show = true
					this.popupObject.desc = '红包兑换该礼品'
					this.giftItem = item
				}
			},
			getShopList(){
				this.$http.post(this.$api.getShopList,{},{token:true}).then(res=>{
					if(res.code == 1){
						this.giftList = res.data
					}
				})
			},
			// 创建订单
			credentials(){
				// 创建订单
				this.$http.post(this.$api.createOrder,this.form1,{token:true}).then(res=>{
					if(res.code == 1){
						this.form.order_sn = res.data.order_sn
						this.credentialsData()
						this.getShopList()
					}
				})
			},
			// 凭证方法
			credentialsData(){
				// clearTimeout(this.timer)
				this.$http.post(this.$api.credentials,this.form,{token:true}).then(res=>{
					if(res.code == 1){
						this.$tools.toast('提交成功')
						// this.$tools.switchTab('/pages/index/index',500)
						// this.timer = setTimeout(function(){
						// 	this.isSubmit = true
						// },500)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.gift-btn-item{
		display: flex;
		align-items: center;
		justify-content: center;
		padding:10rpx;
		background-color: #C52B21;
		border-radius: 10rpx;
		color:#ffffff;
		font-size: 12px;
		// margin-left: 10rpx;
	}
	.gift-list{
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}
	.gift-item:nth-child(2n){
		margin-right: 0;
	}
	.gift-item{
		width: 48%;
		border-radius: 20rpx;
		overflow: hidden;
		margin: 0rpx 20rpx 30rpx 0rpx;
		background-color: #FFF0CA;
		position: relative;
	}
	.gift-item-image{
		width: 100%;
		height: 334rpx;
	}
	
	.gift-item-desc{
		padding: 20rpx;
		background-color: #FFF0CA;
	}
	
	.gift-item-title{
		font-size: 14px;
		color: #333333;
		font-weight: bold;
	}
	
	.gift-item-bottom{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-top: 20rpx;
	}
	
	.gift-item-price{
		font-size: 14px;
		color: #C52B21;
		font-weight: bold;
	}
	
	.gift-item-icon{
		/* 矩形 832 */
		position: absolute;
		left: -6px;
		top: 0px;
		width: 144rpx;
		height: 46rpx;
		font-size: 12px;
		color: #C52B21;
		border-radius: 30rpx 0rpx 30rpx 0px;
		background: #FFF0CA;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 100;
	}
</style>